package com.h2cloud.work;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Data
class Sequence {
    Long id;
    String name;

    @Override
    public String toString() {
        return "(id:" + this.id + ", name：" + this.name + ")";
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class SequenceLevel {
    private Long id;
    private String name;
    private List<SequenceLevel> children;

    @Override
    public String toString() {
        return "(id:" + this.id + ", name：" + this.name + ")";
    }
}

public class sequenceTreeTest {

    public static void main(String[] args) {
        // id一样，视为同一层级
        // a1-b1-c
        List<Sequence> SequenceList1 = new ArrayList<>();
        Sequence sequence11 = new Sequence();
        sequence11.setId(11L);
        sequence11.setName("a1");
        SequenceList1.add(sequence11);
        Sequence sequence12 = new Sequence();
        sequence12.setId(12L);
        sequence12.setName("b1");
        SequenceList1.add(sequence12);
        Sequence sequence13 = new Sequence();
        sequence13.setId(13L);
        sequence13.setName("c");
        SequenceList1.add(sequence13);
        // a1-b1-d-k
        List<Sequence> SequenceList2 = new ArrayList<>();
        Sequence sequence21 = new Sequence();
        sequence21.setId(11L);
        sequence21.setName("a1");
        SequenceList2.add(sequence21);
        Sequence sequence22 = new Sequence();
        sequence22.setId(12L);
        sequence22.setName("b1");
        SequenceList2.add(sequence22);
        Sequence sequence23 = new Sequence();
        sequence23.setId(23L);
        sequence23.setName("d");
        SequenceList2.add(sequence23);
        Sequence sequence24 = new Sequence();
        sequence24.setId(24L);
        sequence24.setName("k");
        SequenceList2.add(sequence24);
        // a2-b12-d2
        List<Sequence> SequenceList3 = new ArrayList<>();
        Sequence sequence31 = new Sequence();
        sequence31.setId(31L);
        sequence31.setName("a2");
        SequenceList3.add(sequence31);
        Sequence sequence32 = new Sequence();
        sequence32.setId(32L);
        sequence32.setName("b12");
        SequenceList3.add(sequence32);
        Sequence sequence33 = new Sequence();
        sequence33.setId(33L);
        sequence33.setName("d2");
        SequenceList3.add(sequence33);

        System.out.println(SequenceList1);
        System.out.println(SequenceList2);
        System.out.println(SequenceList3);
        System.out.println();

        List<SequenceLevel> SequenceLevelTree = new ArrayList<>();
        buildTree(SequenceLevelTree, SequenceList1, 0);
        buildTree(SequenceLevelTree, SequenceList2, 0);
        buildTree(SequenceLevelTree, SequenceList3, 0);
        for (SequenceLevel element : SequenceLevelTree) {
            printTree(element, 0);
        }
    }

    static void buildTree(List<SequenceLevel> SequenceLevelTree, List<Sequence> sequenceList, int index) {
        Sequence sequence = sequenceList.get(index);
        SequenceLevel currentSequenceLevel = null;
        for (SequenceLevel sequenceLevel : SequenceLevelTree) {
            if (sequenceLevel.getId().equals(sequence.getId())) {
                currentSequenceLevel = sequenceLevel;
                break;
            }
        }

        if (currentSequenceLevel == null) {
            currentSequenceLevel = new SequenceLevel(sequence.getId(), sequence.getName(), null);
            SequenceLevelTree.add(currentSequenceLevel);
        }

        if (index + 1 >= sequenceList.size()) {
            return;
        }

        if (CollectionUtils.isEmpty(currentSequenceLevel.getChildren())) {
            currentSequenceLevel.setChildren(new ArrayList<>());
        }
        buildTree(currentSequenceLevel.getChildren(), sequenceList, index + 1);
    }

    static void printTree(SequenceLevel element, int indentLevel) {
        StringBuilder indentation = new StringBuilder();
        for (int i = 0; i < indentLevel; i++) {
            indentation.append("  ");
        }
        indentation.append("-");
        System.out.println(indentation + element.toString());

        if (!CollectionUtils.isEmpty(element.getChildren())) {
            for (SequenceLevel child : element.getChildren()) {
                printTree(child, indentLevel + 1);
            }
        }
    }
}
